/*
 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     bstefanescu
 */
package org.nuxeo.gwt.habyt.app.client.form;

import java.util.ArrayList;
import java.util.List;

import org.nuxeo.gwt.habyt.app.client.Action;
import org.nuxeo.gwt.habyt.invocation.client.PropertyMap;

import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
 * 
 */
public class FormWidget extends SimplePanel implements Form {

    protected List<FormControl> controls = new ArrayList<FormControl>();

    private FormHandler handler;

    protected Action<?> action;

    protected PropertyMap properties;

    public FormWidget(Action<?> action) {
        if (action == null) {
            throw new IllegalArgumentException(
                    "Form Invocation attribute is required");
        }
        this.action = action;
        properties = new PropertyMap();
    }

    @Override
    public PropertyMap getProperties() {
        return properties;
    }

    public Action<?> getAction() {
        return action;
    }

    public FormHandler getHandler() {
        if (handler == null) {
            handler = new DefaultFormHandler();
        }
        return handler;
    }

    public void setHandler(FormHandler handler) {
        this.handler = handler;
    }

    public void addControl(FormControl control) {
        controls.add(control);
    }

    public void removeControl(FormControl control) {
        controls.remove(control);
    }

    public List<FormControl> getControls() {
        return controls;
    }

    public FormControl getControl(String name) {
        for (FormControl control : controls) {
            if (name.equals(control.getName())) {
                return control;
            }
        }
        return null;
    }

    public Widget getControlWidget(String name) {
        FormControl control = getControl(name);
        return control != null ? control.getWidget() : null;
    }

    public Element getControlElement(String name) {
        FormControl control = getControl(name);
        return control != null ? control.getElement() : null;
    }

    public ValidationStatus validate() {
        if (action == null) {
            throw new IllegalStateException("Form invocation was not defined.");
        }
        properties.clear();
        ValidationStatus status = new ValidationStatus();
        for (FormControl control : controls) {
            if (control.validate()) {
                String key = control.getName();
                if (key != null) {
                    properties.put(key, control.getValue());
                }
            } else {
                String msg = control.getErrorMessage();
                if (msg == null) {
                    msg = "Invalid value";
                }
                status.addError(control, msg);
            }
        }
        return status;
    }

    public void submit() {
        clearErrors();
        ValidationStatus status = validate();
        if (!status.isOk()) {
            showErrors(status);
        } else {
            getHandler().submit(this);
        }
    }

    public void reset() {
        getHandler().reset(this);
    }

    protected void showErrors(ValidationStatus status) {
        for (ValidationStatus.Entry entry : status.getErrors()) {
            FormControl control = entry.getControl();
            getHandler().showError(control, entry.getMessage());
        }
    }

    public void clearErrors() {
        for (FormControl control : controls) {
            getHandler().clearError(control);
        }
    }

}
